package edu.pdx.LAMA.AndroidCourse.scheduler;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import edu.pdx.LAMA.AndroidCourse.ModelLayer.EventDetails;
import edu.pdx.LAMA.AndroidCourse.ModelLayer.EventService;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

public class CreateOrModifyEventActivity extends Activity {

	// Creating Extras which we'll be getting from either the activity or the fragment
	public static final String EXTRA_EVENT_ID = "edu.pdx.LAMA.AndroidCourse.scheduler.ID";
	public static final String EXTRA_EVENT_DATE = "edu.pdx.LAMA.AndroidCourse.scheduler.DATE";
	public static final String EXTRA_TITLE = "edu.pdx.LAMA.AndroidCourse.scheduler.TITLE";
	public static final String EXTRA_START_TIME = "edu.pdx.LAMA.AndroidCourse.scheduler.START_TIME";
	public static final String EXTRA_END_TIME = "edu.pdx.LAMA.AndroidCourse.scheduler.END_TIME";
	public static final String EXTRA_PRIORITY = "edu.pdx.LAMA.AndroidCourse.scheduler.PRIORITY";
	public static final String EXTRA_LOCATION = "edu.pdx.LAMA.AndroidCourse.scheduler.LOCATION";
	public static final String EXTRA_CATEGORY = "edu.pdx.LAMA.AndroidCourse.scheduler.CATEGORY";
	public static final String EXTRA_ALARM = "edu.pdx.LAMA.AndroidCourse.scheduler.ALARM";
	public static final String EXTRA_DAILYEVENT = "edu.pdx.LAMA.AndroidCourse.scheduler.DAILYEVENT";

	// for saving the state
	private static final String SAVER_EVENT_ID = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_ID";
	private static final String SAVER_EVENT_DATE = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_DATE";
	private static final String SAVER_TITLE = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_TITLE";
	private static final String SAVER_START_TIME_HOUR = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_START_TIME_HOUR";
	private static final String SAVER_START_TIME_MIN = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_START_TIME_MIN";
	private static final String SAVER_END_TIME_HOUR = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_END_TIME_HOUR";
	private static final String SAVER_END_TIME_MIN = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_END_TIME_MIN";
	private static final String SAVER_PRIORITY = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_PRIORITY";
	private static final String SAVER_LOCATION = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_LOCATION";
	private static final String SAVER_CATEGORY = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_CATEGORY";
	private static final String SAVER_ALARM = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_ALARM";
	private static final String SAVER_DAILYEVENT = "edu.pdx.LAMA.AndroidCourse.scheduler.SAVER_DAILYEVENT";

	private EditText enteredEventTitle;
	//private DatePicker enteredEventDate;
	private TextView displayCurrentDate;
	private Button changeDateButton;
	private TimePicker enteredStartTimePicker;
	private TimePicker enteredEndTimepicker;
	private Spinner enteredPrioritySpinner;
	private EditText enteredLocation;
	private Spinner enteredCategorySpinner;
	private NumberPicker enteredAlarmTime;
	private Button submitEvent;
	private CheckBox enteredCheckBox;

	private int event_id;
	private int eventStartTimeHour;
	private int eventStartTimeMinute;
	private int eventEndTimeHour;
	private int eventEndTimeMinute;
	private int eventAlarm;

	private int day;
	private int month;
	private int year;
	private String changedDate;

	// Alarm stuff
	private PendingIntent mAlarmSender;

	// DB
	private EventService eventService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_or_modify_event);

		// GETTING STUFF FROM THE INTENT
		Intent intent = getIntent();
		event_id = intent.getIntExtra(EXTRA_EVENT_ID, 999999);
		eventService = new EventService(this);

		// Getting the event title that the user has entered
		enteredEventTitle = (EditText) findViewById(R.id.entered_event_title);
		if (event_id != 999999){
			enteredEventTitle.setText(intent.getStringExtra(EXTRA_TITLE));
		}

		//enteredEventDate = (DatePicker) findViewById(R.id.enteredDate);
		if (event_id != 999999){
			String sentDate = intent.getStringExtra(EXTRA_EVENT_DATE);
			String[] parts = sentDate.split("/");
			day = Integer.parseInt(parts[0]);
			month = Integer.parseInt(parts[1]);
			year = Integer.parseInt(parts[2]);
		} else{
			//Calendar calendar = Calendar.getInstance();
			//day = calendar.get(Calendar.DAY_OF_MONTH);
			//month = calendar.get(Calendar.MONTH);
			//year = calendar.get(Calendar.YEAR);
			String sentDate = intent.getStringExtra(EXTRA_EVENT_DATE);
			String[] parts = sentDate.split("/");
			day = Integer.parseInt(parts[0]);
			month = Integer.parseInt(parts[1]);
			year = Integer.parseInt(parts[2]);
		}

		changedDate = day+"/"+month+"/"+year;

		displayCurrentDate = (TextView) findViewById(R.id.dateText);
		displayCurrentDate.setText("Current Date of the Event is: "+changedDate);

		changeDateButton = (Button) findViewById(R.id.changeDateButton);
		changeDateButton.setText("Change Date");
		changeDateButton.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog(999);

			}
		});

		//enteredEventDate.init(year, month, day, null);
		//enteredEventDate.updateDate(year, month, day);
		//enteredEventDate.
		// Setting up the start time of the event
		enteredStartTimePicker = (TimePicker) findViewById(R.id.start_time_picker);
		final Calendar c = Calendar.getInstance();
		if (event_id != 999999){
			// Here we set if we get something
			String sTime = intent.getStringExtra(EXTRA_START_TIME);
			String[] sArray = sTime.split(":");
			enteredStartTimePicker.setCurrentHour(Integer.parseInt(sArray[0]));
			enteredStartTimePicker.setCurrentMinute(Integer.parseInt(sArray[1]));
			eventStartTimeHour = Integer.parseInt(sArray[0]);
			eventStartTimeMinute = Integer.parseInt(sArray[1]);
		}
		else{
			enteredStartTimePicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
			enteredStartTimePicker.setCurrentMinute(c.get(Calendar.MINUTE));
			eventStartTimeHour = c.get(Calendar.HOUR_OF_DAY);
			eventStartTimeMinute = c.get(Calendar.MINUTE);
		}
		// Setting up the listener
		enteredStartTimePicker.setOnTimeChangedListener(new OnTimeChangedListener() {

			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				// TODO Auto-generated method stub
				// the values listened to will be stored in class variables
				// and those class variables would be persisted to db once 
				// the submit button is clicked
				eventStartTimeHour = hourOfDay;
				eventStartTimeMinute = minute;
			}
		});

		// Setting up the end time
		// the events can be 30 minutes or longer or shorter
		// but we start with a defaullt of 30 minutes
		enteredEndTimepicker = (TimePicker) findViewById(R.id.end_time_picker);
		if (event_id != 999999){
			// Here we set if we get something
			String eTime = intent.getStringExtra(EXTRA_END_TIME);
			String[] eArray = eTime.split(":");
			enteredEndTimepicker.setCurrentHour(Integer.parseInt(eArray[0]));
			enteredEndTimepicker.setCurrentMinute(Integer.parseInt(eArray[1]));
			eventEndTimeHour = Integer.parseInt(eArray[0]);
			eventEndTimeMinute = Integer.parseInt(eArray[1]);
		}
		else{
			c.add(Calendar.MINUTE, 30);
			enteredEndTimepicker.setCurrentHour(c.get(Calendar.HOUR));
			enteredEndTimepicker.setCurrentMinute(c.get(Calendar.MINUTE));
			// Setting up the time change listener for the end time
			eventEndTimeHour = c.get(Calendar.HOUR_OF_DAY);
			eventEndTimeMinute = c.get(Calendar.MINUTE);
		}


		enteredEndTimepicker.setOnTimeChangedListener(new OnTimeChangedListener() {

			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				// TODO Auto-generated method stub
				// the values listened to will be stored in class variables
				// and those class variables would be persisted to db once 
				// the submit button is clicked
				eventEndTimeHour = hourOfDay;
				eventEndTimeMinute = minute;
			}
		});


		// setting up the values in the spinner
		enteredPrioritySpinner = (Spinner) findViewById(R.id.priority_spinner);
		List<String> prioSpinnerList = new ArrayList<String>();
		prioSpinnerList.add("Low");
		prioSpinnerList.add("Medium");
		prioSpinnerList.add("High");
		ArrayAdapter<String> prioSpinnerListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, prioSpinnerList);
		prioSpinnerListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		enteredPrioritySpinner.setAdapter(prioSpinnerListAdapter);
		// setting a start value for the spinner if one is there
		if (event_id != 999999){
			enteredPrioritySpinner.setSelection(getIndex(enteredPrioritySpinner, intent.getStringExtra(EXTRA_PRIORITY)));
		}
		else{
			enteredPrioritySpinner.setSelection(0);
		}

		// the spinner get value would be set in the listener for the button

		// setting up the entered location
		// how link this with the places API is an unknown quantity
		// who knows what will happen
		enteredLocation = (EditText) findViewById(R.id.entered_location);
		if (event_id != 999999){
			enteredLocation.setText(intent.getStringExtra(EXTRA_LOCATION));
		}

		// Adding the second spinner which will get us the category under which the task falls
		enteredCategorySpinner = (Spinner) findViewById(R.id.category_spinner);
		List<String> categorySpinnerList = (ArrayList<String>) eventService.getCategories();
		//categorySpinnerList.add("Health");
		//categorySpinnerList.add("Work");
		//categorySpinnerList.add("Leisure");
		ArrayAdapter<String> categorySpinnerListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categorySpinnerList);
		categorySpinnerListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		enteredCategorySpinner.setAdapter(categorySpinnerListAdapter);
		// The spinner get value would be implemented in the listener method for the button
		// setting to an existing value if it exists
		if (event_id != 999999){
			enteredCategorySpinner.setSelection(getIndex(enteredCategorySpinner, intent.getStringExtra(EXTRA_CATEGORY)));
		}
		else{
			enteredCategorySpinner.setSelection(0);
		}

		enteredCheckBox = (CheckBox) findViewById(R.id.dailyEventCheckBox);

		if(event_id != 999999){
			//enteredAlarmTime.setValue(intent.getIntExtra(EXTRA_ALARM, 0));
			enteredCheckBox.setChecked(intent.getBooleanExtra(EXTRA_DAILYEVENT, false));
		}
		else{
			//enteredAlarmTime.setValue(0);
			enteredCheckBox.setChecked(false);
		}

		enteredAlarmTime = (NumberPicker) findViewById(R.id.alarm_time_number_picker);
		enteredAlarmTime.setMinValue(0);
		enteredAlarmTime.setMaxValue(10);
		if(event_id != 999999){
			enteredAlarmTime.setValue(intent.getIntExtra(EXTRA_ALARM, 0));
		}
		else{
			enteredAlarmTime.setValue(0);
		}
		// here we keep track of the change in the value of number picker
		// this would update the value of a class variable, that class variable would
		// be used to set the final alarm time
		enteredAlarmTime.setOnValueChangedListener(new OnValueChangeListener() {

			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				// TODO Auto-generated method stub
				eventAlarm = newVal;
			}
		});

		submitEvent = (Button) findViewById(R.id.submit_button);
		submitEvent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(getApplicationContext(), "Title: "+enteredTitle+"\n", Toast.LENGTH_SHORT).show();
				if(enteredEventTitle.getText().toString() == null || enteredEventTitle.getText().toString().equalsIgnoreCase("")){
					Toast.makeText(getApplicationContext(), "Please Enter a Title", Toast.LENGTH_SHORT).show();
				}
				else if (eventStartTimeHour>eventEndTimeHour){
					Toast.makeText(getApplicationContext(), "Start Time cannot be after end time", Toast.LENGTH_SHORT).show();
				} else if (eventStartTimeHour==eventEndTimeHour && eventStartTimeMinute>eventEndTimeMinute){
						Toast.makeText(getApplicationContext(), "Start Time cannot be after end time", Toast.LENGTH_SHORT).show();
				}
				else {
					persistToDB();

					Intent intt = new Intent(getApplicationContext(), AlarmReceiver.class);
					intt.putExtra(AlarmReceiver.EXTRA_TITLE, enteredEventTitle.getText().toString());
					intt.putExtra(AlarmReceiver.EXTRA_CONTENT, enteredLocation.getText().toString());
					if(event_id!=999999){
						Log.d("TAG", "event id while creating alarm "+event_id);
						mAlarmSender = PendingIntent.getBroadcast(getApplicationContext(), event_id, intt, 0);
						//eventService.updateEvent(details);
					} else {
						//EventService eventService = new EventService(getApplicationContext());
						int latest_id = getLatestId(eventService);
						Log.d("TAG", "the event id is "+latest_id);
						mAlarmSender = PendingIntent.getBroadcast(getApplicationContext(), latest_id, intt, 0);
					}
					//eventService.addEvent(details);
					//tryAlarm();
					//mAlarmSender = PendingIntent.getBroadcast(getApplicationContext(), eve, new Intent(getApplicationContext(), AlarmReceiver.class), 0);
					startAlarm();
				}
			}
		});

		if (savedInstanceState != null) {
			event_id = savedInstanceState.getInt(SAVER_EVENT_ID);
			enteredEventTitle.setText(savedInstanceState.getString(SAVER_TITLE));

			changedDate = savedInstanceState.getString(SAVER_EVENT_DATE);
			displayCurrentDate.setText("Current Date of the Event is: "+changedDate);
			String[] returnDateArray = changedDate.split("/");
			day = Integer.parseInt(returnDateArray[0]);
			month = Integer.parseInt(returnDateArray[1]);
			year = Integer.parseInt(returnDateArray[2]);

			eventStartTimeHour = savedInstanceState.getInt(SAVER_START_TIME_HOUR);
			enteredStartTimePicker.setCurrentHour(eventStartTimeHour);
			eventStartTimeMinute = savedInstanceState.getInt(SAVER_START_TIME_MIN);
			enteredStartTimePicker.setCurrentMinute(eventStartTimeMinute);

			eventEndTimeHour = savedInstanceState.getInt(SAVER_END_TIME_HOUR);
			enteredEndTimepicker.setCurrentHour(eventEndTimeHour);
			eventEndTimeMinute = savedInstanceState.getInt(SAVER_END_TIME_MIN);
			enteredEndTimepicker.setCurrentMinute(eventEndTimeMinute);

			enteredPrioritySpinner.setSelection(getIndex(enteredPrioritySpinner, savedInstanceState.getString(SAVER_PRIORITY)));
			enteredLocation.setText(savedInstanceState.getString(SAVER_LOCATION));
			enteredCategorySpinner.setSelection(getIndex(enteredCategorySpinner, savedInstanceState.getString(SAVER_CATEGORY)));
			enteredAlarmTime.setValue(savedInstanceState.getInt(SAVER_ALARM));
			enteredCheckBox.setChecked(savedInstanceState.getBoolean(SAVER_DAILYEVENT));
		}

	}

	/*private void tryAlarm() {
		Log.d("hello", "coming inside the alarm setting method");
		// TODO Auto-generated method stub
		AlarmManager mgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
		Intent i = new Intent(this, AlarmReceiver.class);
		PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
		mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+50, pi);
	}*/

	public void persistToDB(){
		Toast.makeText(getApplicationContext(), "Title: "+enteredEventTitle.getText().toString()+"\n"
				+"Date Event: "+changedDate+"\n"
				+"start Time: "+eventStartTimeHour+":"+eventStartTimeMinute+"\n"
				+"event end: "+eventEndTimeHour+":"+eventEndTimeMinute+"\n"
				+"priority: "+enteredPrioritySpinner.getSelectedItem().toString()+"\n"
				+"location: "+enteredLocation.getText().toString()+"\n"
				+"category: "+enteredCategorySpinner.getSelectedItem().toString()+"\n"
				+"alarm: "+eventAlarm, Toast.LENGTH_LONG).show();

		EventDetails details = new EventDetails();

		details.setTitle(enteredEventTitle.getText().toString());
		details.setDate(changedDate);
		details.setStartTime(eventStartTimeHour+":"+eventStartTimeMinute);
		details.setEndTime(eventEndTimeHour+":"+eventEndTimeMinute);
		details.setPriority(enteredPrioritySpinner.getSelectedItem().toString());
		details.setLocation(enteredLocation.getText().toString());
		details.setCategory(enteredCategorySpinner.getSelectedItem().toString());
		details.setAlarmTime(eventAlarm);
		ArrayList<Integer> idList;
		ArrayList<Integer> otherTableList;
		if(enteredCheckBox.isChecked()){
			idList = eventService.getAllDailyEventIds();
			if(idList.contains(event_id)){
				// update the daily event
				details.setID(event_id);
				eventService.updateDailyEvent(details);
			}
			else{
				// create a new daily event after getting a new max id
				details.setID(eventService.getDailyEventMaxID()+1);
				eventService.addDailyEvent(details);
				// deleting from the other table if it exists
				otherTableList = eventService.getEventIds();
				Log.d("create or modify", "1.length of array "+otherTableList.size());
				if (otherTableList.contains(event_id)){
					eventService.deleteEvent(event_id);
				}
			}
		}
		else{
			Log.d("adfsrhgbzdfbz", "asdfghjklpoiuytrewq");
			idList = eventService.getEventIds();
			if(idList.contains(event_id)){
				// update
				details.setID(event_id);
				eventService.updateEvent(details);
			}
			else{
				// create

				details.setID(getLatestId(eventService));
				eventService.addEvent(details);
				// deleting from the other table if it exists
				otherTableList = eventService.getAllDailyEventIds();
				Log.d("create or modify", "2.length of array "+otherTableList.size());
				Iterator<Integer> iterator = otherTableList.iterator();
				while(iterator.hasNext()){
					Log.d("create or modify", "element: "+iterator.next());
				}
				Log.d("create or modify", "id to delete: "+event_id);
				if (otherTableList.contains(event_id)){
					eventService.deleteDailyEvent(event_id);
				}
			}
		}
	}

	private int getLatestId(EventService eventService) {
		// TODO Auto-generated method stub
		return (eventService.getMaxID()+1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.create_or_modify_event, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private int getIndex(Spinner spinner, String myString){
		int index = 0;
		for (int i=0;i<spinner.getCount();i++){
			if (spinner.getItemAtPosition(i).equals(myString)){
				index = i;
			}
		}
		return index;
	}

	//@SuppressWarnings("deprecation")
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		if (id == 999){
			return new DatePickerDialog(this, myDateListener, year, month, day);
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			// TODO Auto-generated method stub
			changedDate = dayOfMonth+"/"+monthOfYear+"/"+year;
			displayCurrentDate.setText("Current Date of the Event is: "+dayOfMonth+"/"+monthOfYear+"/"+year);
		}
	};

	public void startAlarm() {
		Calendar c = Calendar.getInstance();
		String[] s = changedDate.split("/");
		Log.d("TAG", "the date used to set alarm"+changedDate);
		int lDay = Integer.parseInt(s[0]);
		int lMonth = Integer.parseInt(s[1]);
		int lYear = Integer.parseInt(s[2]);
		Log.d("TAG", "ASdfghnbfvdsxasdxgnbv"+lDay);
		c.set(Calendar.DAY_OF_MONTH, lDay);
		c.set(Calendar.MONTH, lMonth);
		c.set(Calendar.YEAR, lYear);
		Log.d("TAG", "event start time hour while setting the alarm "+eventStartTimeHour);
		Log.d("TAG", "event start time minute while setting the alarm "+eventStartTimeMinute);

		c.set(Calendar.HOUR_OF_DAY, eventStartTimeHour);
		c.set(Calendar.MINUTE, eventStartTimeMinute);
		c.set(Calendar.SECOND, 0);
		Log.d("TAG", "second before set"+((Integer)c.get(Calendar.SECOND)).toString());
		//c.add(Calendar.SECOND, 10);
		c.add(Calendar.MINUTE, -(eventAlarm));

		Log.d("TAG", "God knows whats happening here");
		Log.d("TAG", "day after set: "+((Integer)c.get(Calendar.DAY_OF_MONTH)).toString());
		Log.d("TAG", "month after set: "+((Integer)c.get(Calendar.MONTH)).toString());
		Log.d("TAG", "year after set: "+((Integer)c.get(Calendar.YEAR)).toString());
		Log.d("TAG", "hour after set: "+((Integer)c.get(Calendar.HOUR)).toString());
		Log.d("TAG", "minute after set"+((Integer)c.get(Calendar.MINUTE)).toString());
		Log.d("TAG", "second after set"+((Integer)c.get(Calendar.SECOND)).toString());
		//c.set(Calendar.DAY_OF_MONTH, );

		long firstTime = c.getTimeInMillis();
		AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
		am.set(AlarmManager.RTC_WAKEUP, firstTime, mAlarmSender);

		// finishing activity here
		// dont know if this will work or not
		//CreateOrModifyEventActivity.this.finish();
		// change to above line if below change doesn't work
		Intent ix = new Intent(this, ScheduleActivity.class);
		startActivity(ix);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);

		outState.putInt(SAVER_EVENT_ID, event_id);
		outState.putString(SAVER_TITLE, enteredEventTitle.getText().toString());
		outState.putString(SAVER_EVENT_DATE, changedDate);
		outState.putInt(SAVER_START_TIME_HOUR, eventStartTimeHour);
		outState.putInt(SAVER_START_TIME_MIN, eventStartTimeMinute);
		outState.putInt(SAVER_END_TIME_HOUR, eventEndTimeHour);
		outState.putInt(SAVER_END_TIME_MIN, eventEndTimeMinute);
		outState.putString(SAVER_PRIORITY, enteredPrioritySpinner.getSelectedItem().toString());
		outState.putString(SAVER_LOCATION, enteredLocation.getText().toString());
		outState.putString(SAVER_CATEGORY, enteredCategorySpinner.getSelectedItem().toString());
		outState.putInt(SAVER_ALARM, eventAlarm);
		outState.putBoolean(SAVER_DAILYEVENT, enteredCheckBox.isChecked());
	}
}
